package com.infore.authorityManagement.controller;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.infore.authorityManagement.entity.BusinessConstant;
import com.infore.authorityManagement.entity.SysAppInfo;
import com.infore.authorityManagement.enums.ResultStatus;
import com.infore.authorityManagement.service.ISysAppInfoService;
import com.infore.authorityManagement.util.Page;
import com.infore.authorityManagement.util.ResultEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: ApplicationController
 * @Description: 应用管理controller类，对外接口
 * @author 谭红波
 * @date 2017年12月19日
 * 
 */
@Api(value = "应用系统管理", tags = "应用系统管理")
@Controller
@RequestMapping("/apps")
public class AppController {
    @Autowired
    ISysAppInfoService sysAppInfoService;

    @ApiOperation(value = "查询数据目录", notes = "根据id查询数据目录")
    @RequestMapping(value = "/id", method = RequestMethod.GET)
    public @ResponseBody ResultEntity getAppInfoById(String appId, String token) {
        SysAppInfo app = sysAppInfoService.getSysAppInfoById(appId);
        if (app == null) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.APP_NOT_EXIST, null);
        }
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.QUERY_SUCCESS).setInfo(app);
    }

    @ApiOperation(value = "id查询系统集合", notes = "根据id查询数据目录")
    @RequestMapping(value = "/ids", method = RequestMethod.GET)
    public @ResponseBody ResultEntity getAppInfoById(@RequestParam(value = "appId", required = false) List<String> appId, String keyword) {
        List<SysAppInfo> list = sysAppInfoService.getSysAppInfosById(appId, keyword);
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.QUERY_SUCCESS).setDataList(list);
    }

    @ApiOperation(value = "批量查询应用系统", notes = "查询所有应用系统信息")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public @ResponseBody ResultEntity getAppsByTypeOrAppName(SysAppInfo appInfo, Integer pageSize, Integer pageNo,
            String token) {
        if (pageSize == null || pageNo == null || pageSize < 1 || pageNo < 0) {
            List<SysAppInfo> list = sysAppInfoService.getSysAppInfos(appInfo);
            return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.QUERY_SUCCESS).setDataList(list);
        } else {
            Page<SysAppInfo> page = sysAppInfoService.getSysAppInfosForPage(appInfo, pageSize, pageNo);
            return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.QUERY_SUCCESS).setPage(page);
        }
    }

    @ApiOperation(value = "新增应用系统信息", notes = "新增应用系统信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody ResultEntity addAppInfo(SysAppInfo appInfo, String token) {
        String appName = appInfo.getAppName();
        String appPort = appInfo.getAppPort();
        String appCode = appInfo.getAppCode();

        if (StringUtils.isBlank(appName) || StringUtils.isBlank(appPort) || StringUtils.isBlank(appCode)) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.PARAM_ERROR);
        }

        java.util.Date date = new java.util.Date();
        long d = date.getTime();
        Timestamp timestamp = new Timestamp(d);
        appInfo.setCreateTime(timestamp);
        Map<String, Object> map = sysAppInfoService.addSysAppInfo(appInfo);
        int resultIdt = (int) map.get("resultIdt");
        if (resultIdt == -1) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.APP_EXIST);
        }
        if (resultIdt == -2) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.DATA_ERROR);
        }
        
        if (resultIdt == -3) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.APP_NOT_EXIST);
        }
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.INSERT_SUCCESS).setMapValue("appId", map.get("appId"));
    }

    @ApiOperation(value = "修改应用系统信息", notes = "修改应用系统信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public @ResponseBody ResultEntity updateAppInfo(SysAppInfo appInfo, String token) {
        String appId = appInfo.getAppId();
        if (appId == null || appId.trim().isEmpty()) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.PARAM_ERROR);
        }
        int resultIdt = sysAppInfoService.updateSysAppInfo(appInfo);
        if (resultIdt == -1) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.APP_EXIST);
        }
        if (resultIdt == -2) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.DATA_ERROR);
        }
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.UPDATE_SUCCESS);
    }

    @ApiOperation(value = "删除应用系统信息", notes = "删除应用系统信息")
    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public @ResponseBody ResultEntity deleteAppInfo(String appId, String token) {
        int resultIdt = sysAppInfoService.deleteSysAppInfo(appId);
        if (resultIdt == -1) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.APP_NOT_EXIST);
        }
        if (resultIdt == -2) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.DATA_ERROR);
        }
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.DELETE_SUCCESS);

    }

}
